import {queryGoodsList,queryGoodMenuList} from "../../../http/api";
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowDetail:false,
    menusArr:[],
    goodsArr:[],
    currentMenuId:"",
    currentMenuName:"",
    goodsObj:{},
    cartArr:[],
    orderType:1,
    merchantId:wx.getStorageSync('merchantId'),

    showBall:false,
    position:{
      startX:0,
      startY:0
    }

    

  },
  toPage(e){
    const {url} = e.currentTarget.dataset
   
    wx.navigateTo({
      url
    })
  },

  handleMenuClick(e){
    const {id} = e.currentTarget.dataset;
    this.setData({
      currentMenuId:id
    })
    this.getGoods()
  },

  handleDelete(e){
    const deleteArr = e.detail

    const cartArr = this.data.cartArr.filter((item,index)=>{
      return deleteArr.indexOf(index+'')==-1
    })

    this.setData({
      cartArr
    })
    // console.log(deleteArr)
  },

  getGoods(){
    wx.showLoading();
    queryGoodsList({goodMenuId:this.data.currentMenuId}).then(goodsRes=>{
      
      if(goodsRes.data.code === 200){
        this.setData({
          goodsArr:[...goodsRes.data.data]
        })

      }
    }).finally(()=>{
      wx.hideLoading()
    })

  },

  handleCloseDetail(){
    this.setData({
      isShowDetail:false
    })
  },

  handleShowGoodsDetail(e){
    const {item} = e.currentTarget.dataset;

    this.setData({
      isShowDetail:true,
      goodsObj:item
    })
 
  },

  handleUpdateCart(e){
    const {index,num} = e.detail
    this.setData({
      [`cartArr[${index}].num`]:num
    })
   
  },

  handleAddCart(e){
    let goodsObj = {}
    let num = 0

    if(e.type==="tap"){
      goodsObj = e.currentTarget.dataset.item;
      num = e.currentTarget.dataset.num;
    }else{
      goodsObj= e.detail.currentTarget.dataset.item;
      num= e.detail.currentTarget.dataset.num;
    }


    this.setData({
      "position.startX": e.touches[0].clientX,
      "position.startY": e.touches[0].clientY,
      showBall: true
    });
      
    const cartArr = this.data.cartArr;
    console.log("cartArr",cartArr.length)
    let goodsIndex = "";
    const arr = cartArr.filter((item,index)=>{
      if(item.id === goodsObj.id){
        goodsIndex=index
      }
      return item.id === goodsObj.id
    })

    if(arr.length>0){
      const newNum = parseInt(this.data.cartArr[goodsIndex].num)+parseInt(num)
      this.setData({
        [`cartArr[${goodsIndex}].num`]:newNum
      })
    }else{
      goodsObj.num = parseInt(num)
      cartArr.push(goodsObj)
      this.setData({
        cartArr
      })
    }
    // wx.showToast({
    //   title: '已添加至购物车',
    //   icon:'none'
    // })

    this.setData({
      isShowDetail:false
    })  
  },
  animationHasDone(){
    // console.log("animationHasDone")
    this.setData({
      showBall:false
    })
  },

  async initPageData(){

    wx.showLoading()

    const menuRes = await queryGoodMenuList({merchantId:this.data.merchantId,orderType:wx.getStorageSync('orderType')})
 
    if(menuRes.data.code===200){
  
      this.setData({
        menusArr:[...menuRes.data.data]
      })
      
      this.setData({
        currentMenuId:this.data.menusArr[0].id,
        currentMenuName:this.data.menusArr[0].name
      })

      const goodsRes = await queryGoodsList({goodMenuId:this.data.currentMenuId,orderType:wx.getStorageSync('orderType')})
    
      
      if(goodsRes.data.code ===200){
      
        this.setData({
          goodsArr:[...goodsRes.data.data]
        })
      }

     
    }
    

    wx.hideLoading()



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    if(options.orderType==0 || options.orderType){
      // if(wx.getStorageSync('orderType') != options.orderType){
      //   wx.setStorageSync('cart', '[]') 
      // }
      wx.setStorageSync('cart', [])
      wx.setStorageSync('orderType', options.orderType);
      
    }
    this.initPageData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const orderType  = wx.getStorageSync('orderType')
    if(orderType==0 || orderType){
      this.setData({
        orderType
      });
    }
    const cartArr = app.getCartData()
    this.setData({
      cartArr,
      
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})